/*
 * @Description:
 * @Author: YangWenFeng
 * @Date: 2022-10-04 17:16:32
 * @LastEditTime: 2022-10-22 18:32:37
 * @LastEditors: YangWenFeng
 */
#include "mqtt.h"
#include "AT.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"

char Send_Buf[200];

void Mqtt_Link_(void)
{
    while (muc_At_Send_To_Device(&sys_AT_, "AT\r\n", "OK", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+ECRST\r\n", "OK", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+CFUN=1\r\n", "OK", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+CEREG?\r\n", "OK", "+CEREG: 0,", 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+CGATT=1\r\n", "OK", "AT+CGATT=1", 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+ECMTCFG=\"cloud\",0,1,1\r\n", "OK", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+ECMTOPEN=0,\"mqtt.heclouds.com\",6002\r\n", "+ECMTOPEN: 0,0", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+ECMTCONN=0,\"设备ID\",\"产品ID\",\"鉴权密钥\"\r\n", "+ECMTCONN: 0,0,0", NULL, 3000, 2))
        ;
    while (muc_At_Send_To_Device(&sys_AT_, "AT+ECMTPUB=0,12345,0,0,\"$dp\",\"{\"datastreams\":[{\"id\":\"EC_01\", \"datapoints\":[{\"value\": 23.1}]}]}\"\r\n",
                                 "+ECMTPUB: 0,12345,0", NULL, 1000, 0))
        ;
}

void Send_Int_Data_To_Mqtt(char *name, int Val)
{
    int len = 0;
    //	if(sys_AT_.Seng_Flag){
    sprintf(Send_Buf,
            "AT+ECMTPUB=0,12345,0,0,\"$dp\",\"{\"datastreams\":[{\"id\":\"%s\", \"datapoints\":[{\"value\": %d}]}]}\"\r\n",
            name, Val);
    while (!muc_At_Send_To_Device(&sys_AT_, Send_Buf, "+ECMTPUB: 0,12345,0", NULL, 1000, 2))
        ;
    //	}
}
